Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Set Mismatch

Problem Statement

You are given an array nums of size n, originally containing 1 to n integers. Unfortunately, due to some error, one number got duplicated, replacing another number that now goes missing from the sequence.

Identify the duplicated number and the missing number and return them as a pair.

Examples

  • Example 1:

    • Input: nums = [1,3,3,4]
    • Expected Output: [3,2]
    • Justification: Here, 3 appears twice, and 2 is missing from the sequence.
  • Example 2:

    • Input: nums = [2,2,3,5,4]
    • Expected Output: [2,1]
    • Justification: 2 is duplicated, and 1 is missing, making 2 the repeated number and 1 the missing one.
  • Example 3:

    • Input: nums = [1,5,3,2,7,7,6]
    • Expected Output: [7,4]
    • Justification: In this sequence, 7 is the number that appears twice, and 4 is absent, indicating 7 as the duplicate and 4 as the missing number.

Solution

To solve this problem, we'll employ an approach that meticulously scans through the given array to identify both the duplicate and the missing numbers. This method is efficient because it doesn't require sorting or additional data structures that significantly increase complexity. The core idea revolves around using the properties of numbers and their indices in the array, thus ensuring a solution that is both space and time-efficient.

Our strategy includes iterating over the array and marking visited numbers by flipping the sign of the number at the index corresponding to each value encountered. This technique helps us detect the duplicate number when we stumble upon a value pointing to an index with an already negative number. For finding the missing number, we'll go through the array once more to find the first positive number, indicating the index of the missing number. This approach stands out for its direct manipulation of the array, reducing the need for extra space and ensuring a quick solution.

Step-by-Step Algorithm

  1. Initialize Variables: Start by initializing two variables, duplicate to -1 (to store the duplicated number) and missing to -1 to store missing number.

  2. Identify Duplicate: Loop through each number n in the input array.

    • Calculate the index i as the absolute value of n minus 1 (since arrays are 0-indexed).
    • If the value at index i in the array is negative, this means n is the duplicated number (as we have visited this index before). Set duplicate to n.
    • Otherwise, mark the value at index i as visited by multiplying it by -1.
  3. Find Missing Number: Loop through the array again to find the missing number.

    • For each index i, check if the value at i is positive.
    • If found positive, it means this index (representing i+1 number) was never visited, i.e., i+1 is the missing number. Set missing to i+1.
  4. Return Result: Return an array containing duplicate and missing as the first and second elements, respectively.

Algorithm Walkthrough

Let's consider the Input: [1,5,3,2,7,7,6]

  1. Initialize Variables:

    • duplicate = -1
    • missing = -1
  2. Identify Duplicate:

    • Start with the original array: [1, 5, 3, 2, 7, 7, 6]

    • Iteration 1: Process n = 1, marking index 0 (1-1=0). Updated array: [-1, 5, 3, 2, 7, 7, 6].

    • Iteration 2: Process n = 5, marking index 4 (5-1=4). Updated array: [-1, 5, 3, 2, -7, 7, 6].

    • Iteration 3: Process n = 3, marking index 2 (3-1=2). Updated array: [-1, 5, -3, 2, -7, 7, 6].

    • Iteration 4: Process n = 2, marking index 1 (2-1=1). Updated array: [-1, -5, -3, 2, -7, 7, 6].

    • Iteration 5: Process n = 7, marking index 6 (7-1=6). Updated array: [-1, -5, -3, 2, -7, 7, -6].

    • Iteration 6: Process n = 7 again, find that index 6 is already negative, thus identifying 7 as the duplicate. The array remains unchanged: [-1, -5, -3, 2, -7, 7, -6].

    • Iteration 7: Process n = 6, marking index 5 (6-1=5). updated array: [-1, -5, -3, 2, -7, -7, -6].

  3. Find Missing Number:

    • Revisiting the correctly updated array: [-1, -5, -3, 2, -7, -7, -6], we look for the first positive number.

    • The positive number is at index 3 (2), indicating that the number 4 (index+1) is missing from the original array.

  4. Return Result:

    • The algorithm identifies 7 as the duplicate and 4 as the missing number, so the result returned is [7, 4].
Image

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity:

The algorithm iterates through the array of numbers twice. The first iteration is to identify the duplicate number by marking the indices visited (negative marking). The second iteration is to find the missing number by checking for a positive value in the modified array. Since each iteration goes through the array once, and there are two iterations, the time complexity is linear with respect to the size of the input array, n.

Space Complexity:

The solution modifies the input array in place to track visited numbers and does not use any additional data structures that grow with the input size. The space used for the output and a few variables does not depend on the input size, making the space complexity constant, .

Set Mismatch (easy)
Is Subsequence (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity:

Space Complexity: